package com.drama.api.company.order.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.drama.api.base.Type;
import com.drama.api.company.dramas.dao.CompanyDramasDao;
import com.drama.api.company.dramas.domain.po.CompanyDramasPO;
import com.drama.api.company.order.dao.OrderConsumeDao;
import com.drama.api.company.order.domain.vo.*;
import com.drama.api.company.order.service.ICompanyOrderService;
import com.drama.api.sys.order.domain.vo.SysCompanyOrderConsumeSearchVO;
import com.drama.api.sys.order.domain.vo.SysCompanyOrderConsumeVO;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class CompanyOrderServiceImpl implements ICompanyOrderService {

    @Autowired
    private OrderConsumeDao orderConsumeDao;
    @Autowired
    private CompanyDramasDao companyDramasDao;

    @Override
    public CompanyOrderCountVO getCount(CompanyOrderCountSearchVO searchVO) {
//        System.out.println(DateUtil.beginOfDay(DateUtil.yesterday()).toString());
//        System.out.println(DateUtil.endOfDay(DateUtil.yesterday()).toString());
//        System.out.println(DateUtil.beginOfMonth(DateUtil.date()).toString());
//        System.out.println(DateUtil.endOfMonth(DateUtil.date()).toString());
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        CompanyOrderCountVO countVO=new CompanyOrderCountVO();
        //查询总统计
        countVO.setTotalNum(orderConsumeDao.getCount(searchVO));
        //查询当日统计
        searchVO.setStartDate(DateUtil.beginOfDay(DateUtil.yesterday()).toString());
        searchVO.setEndDate(DateUtil.endOfDay(DateUtil.yesterday()).toString());
        countVO.setYesterdayNum(orderConsumeDao.getCount(searchVO));
        //查询当月统计
        searchVO.setStartDate(DateUtil.beginOfMonth(DateUtil.date()).toString());
        searchVO.setEndDate(DateUtil.endOfMonth(DateUtil.date()).toString());
        countVO.setThisMonthNum(orderConsumeDao.getCount(searchVO));
        return countVO;
    }

    @Override
    public List<CompanyOrderListVO> getCountList(CompanyOrderListSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        if(StringUtils.isNotBlank(searchVO.getStartDate())){
            searchVO.setStartDate(DateUtil.beginOfDay(DateUtil.parseDate(searchVO.getStartDate())).toString());
        }
        if(StringUtils.isNotBlank(searchVO.getEndDate())){
            searchVO.setEndDate(DateUtil.endOfDay(DateUtil.parseDate(searchVO.getEndDate())).toString());
        }
        return orderConsumeDao.getCountList(searchVO);
    }

    @Override
    public List<CompanyOrderVO> getList(CompanyOrderSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        if(StringUtils.isNotBlank(searchVO.getSearchDate())){
            searchVO.setStartDate(DateUtil.beginOfDay(DateUtil.parseDate(searchVO.getSearchDate())).toString());
            searchVO.setEndDate(DateUtil.endOfDay(DateUtil.parseDate(searchVO.getSearchDate())).toString());
        }
        if(StringUtils.isNotBlank(searchVO.getStartDate())){
            searchVO.setStartDate(DateUtil.beginOfDay(DateUtil.parseDate(searchVO.getStartDate())).toString());
        }
        if(StringUtils.isNotBlank(searchVO.getEndDate())){
            searchVO.setEndDate(DateUtil.endOfDay(DateUtil.parseDate(searchVO.getEndDate())).toString());
        }
        return orderConsumeDao.getList(searchVO);
    }

    @Override
    public List<CompanyOrderDramaVO> getDramaList(CompanyOrderDramaSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        return companyDramasDao.getOrderDramaList(searchVO);
    }

    @Override
    public CompanyOrderDramaVO getDramaOne(String id) {
        if(StringUtils.isBlank(id)){
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        CompanyDramasPO companyDramasPO=companyDramasDao.selectByPrimaryKey(id);
        if(companyDramasPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        CompanyOrderDramaVO companyOrderDramaVO=new CompanyOrderDramaVO();
        companyOrderDramaVO.setId(companyDramasPO.getId());
        companyOrderDramaVO.setDramaImg(companyDramasPO.getDramaImg());
        companyOrderDramaVO.setDramaName(companyDramasPO.getDramaName());
        companyOrderDramaVO.setDramaNum(companyDramasPO.getDramaNum());
        companyOrderDramaVO.setDramaState(companyDramasPO.getDramaState());
        return companyOrderDramaVO;
    }



    @Override
    public List<SysCompanyOrderConsumeVO> getCompanyList(SysCompanyOrderConsumeSearchVO searchVO) {
        return orderConsumeDao.getCompanyList(searchVO);
    }
}